﻿using System;
using System.Collections.Generic;
using System.Text;

using MapCenterSystem.DataInfo;
using System.Data.SqlClient;
using MapCenterSystem.Configuration;
using System.Data;
using MapCenterSystem.DefineType;

namespace MapCenterSystem.DataAccess
{
    public class GraphNodeServiceEx: GraphNodeService
    {
        public List<GraphNodeInfoEx> GetListNodeRelate(Coordinate start)
        {
            using (SqlConnection sqlConnect = new SqlConnection(GlobalConfig.ConnectionString))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "sp_StreetSegment_GetSegmentRelation";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = sqlConnect;
                command.CommandTimeout = GlobalConfig.TimeOut;
                command.Parameters.AddWithValue("@x", start.Latitude);
                command.Parameters.AddWithValue("@y", start.Longitude);
                try
                {
                    sqlConnect.Open();
                }
                catch
                {
                    throw new Exception("Can not connect to database. Please check connect");
                }
                SqlDataReader reader = command.ExecuteReader();
                List<GraphNodeInfoEx> result = new List<GraphNodeInfoEx>(); 
                while (reader.Read())
                {
                    GraphNodeInfoEx graphNodeRelate = new GraphNodeInfoEx();
                    graphNodeRelate.Id = Convert.ToInt32(reader["ID"]);
                    graphNodeRelate.Location = new Coordinate(Convert.ToDouble(reader["xLocation"]), Convert.ToDouble(reader["yLocation"]));
                    graphNodeRelate.IDStreet = Convert.ToInt32(reader["IDStreet"]);
                    graphNodeRelate.Length = (int)Coordinate.Distance(start, graphNodeRelate.Location);
                    result.Add(graphNodeRelate);
                }
                return result;
            }
            
        }

        public int IsRelated(Coordinate node1, Coordinate node2)
        {
            using (SqlConnection sqlConnect = new SqlConnection(GlobalConfig.ConnectionString))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "select dbo.IsRelated(@x1, @y1, @x2, @y2)";
                command.Connection = sqlConnect;
                command.CommandTimeout = GlobalConfig.TimeOut;
                command.Parameters.AddWithValue("@x1", node1.Latitude);
                command.Parameters.AddWithValue("@y1", node1.Longitude);
                command.Parameters.AddWithValue("@x2", node2.Latitude);
                command.Parameters.AddWithValue("@y2", node2.Longitude);
                try
                {
                    sqlConnect.Open();
                }
                catch
                {
                    throw new Exception("Can not connect to database. Please check connect");
                }
                return (int)command.ExecuteScalar();
            }

        }

        public GraphNodeInfoEx GetGraphNodeInfoEx(Coordinate location)
        {
            GraphNodeInfoEx result = new GraphNodeInfoEx();
            result.Location = location;
            return result;

        }
        /*
        public DataInfo.MyLocation GetStressNearMyLocation(double latatude, double longtatude)
        {
            using (SqlConnection sqlConnect = new SqlConnection(GlobalConfig.ConnectionString))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "sp_StreetSegment_GetSegmentRelation";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = sqlConnect;
                command.CommandTimeout = GlobalConfig.TimeOut;
                command.Parameters.AddWithValue("@x", latatude);
                command.Parameters.AddWithValue("@y", longtatude);
                try
                {
                    sqlConnect.Open();
                }
                catch
                {
                    throw new Exception("Can not connect to database. Please check connect");
                }
                SqlDataReader reader = command.ExecuteReader();
                MyLocation lClass = new MyLocation();
                while (reader.Read())
                {
                    lClass.Id = Convert.ToInt32(reader["ID"]);
                    lClass.XLocation = Convert.ToDouble(reader["xLocation"]);
                    lClass.YLocation = Convert.ToDouble(reader["yLocation"]);
                    //lClass.IdStreet = Convert.ToInt32(reader[4]);
                }
                return lClass;
            }
        }
         * */
    }
}
